﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;

namespace PresentationProject
{
    class ImageObject : Image
    {
        public ImageObject()
            : base()
        {
            
            //Visibility = System.Windows.Visibility.Hidden;
            //InitializeComponent();
        }

        private string id;
        public string Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        public void draw(Storyboard sb)
        {
            rotate(sb, 80, 0, 3);
            changeSize(sb, 0.3, 0.3, 1.0, 1.0, 3);
            appearslowly(sb, 5);
            Visibility = System.Windows.Visibility.Visible;
        }

        public void disappear()
        {
            Visibility = System.Windows.Visibility.Hidden;
        }

        /* 图片逐渐出现的动画，
         * sb为新建动画所在的故事版， time为这段动画的持续时间（以秒为单位）*/
        public void appearslowly(Storyboard sb, int time)
        {
            DoubleAnimation imOpacity = new DoubleAnimation();
            imOpacity.To = 1.0;
            imOpacity.From = 0.0;
            imOpacity.Duration = TimeSpan.FromSeconds(time);
            Storyboard.SetTargetName(imOpacity, Name);
            Storyboard.SetTargetProperty(imOpacity, new PropertyPath(ImageObject.OpacityProperty));
            sb.Children.Add(imOpacity);
        }

        /* 图片逐渐出现的动画， 
         * time为这段动画的持续时间(以秒为单位)*/
        public void appearslowly(double time)
        {
            DoubleAnimation imOpacity = new DoubleAnimation();
            imOpacity.To = 1.0;
            imOpacity.From = 0.0;
            imOpacity.Duration = TimeSpan.FromSeconds(time);
            BeginAnimation(OpacityProperty, imOpacity);
        }

        /* 图片移动的动画
         * to为Margin属性， time为这段动画的持续时间(以秒为单位)*/
        public void move(Thickness to, double time)
        {
            ThicknessAnimation imTA = new ThicknessAnimation();
            imTA.To = to;
            imTA.Duration = TimeSpan.FromSeconds(time);
            BeginAnimation(ImageObject.MarginProperty, imTA);
        }

        /* 图片移动的动画
         * from和to为Margin属性， time为这段动画的持续时间(以秒为单位)*/
        public void move(Thickness from, Thickness to, double time)
        {
            ThicknessAnimation imTA = new ThicknessAnimation();
            imTA.From = from;
            imTA.To = to;
            imTA.Duration = TimeSpan.FromSeconds(time);
            BeginAnimation(ImageObject.MarginProperty, imTA);
        }

        /* 图片移动的动画
         * sb为新建动画所在的故事版， from和to为Margin属性， 
         * time为这段动画的持续时间（以秒为单位）*/
        public void move(Storyboard sb, Thickness from, Thickness to, double time)
        {
            ThicknessAnimation imTA = new ThicknessAnimation();
            imTA.From = from;
            imTA.To = to;
            imTA.Duration = TimeSpan.FromSeconds(time);
            Storyboard.SetTargetName(imTA, Name);
            Storyboard.SetTargetProperty(imTA, new PropertyPath(ImageObject.MarginProperty));
            sb.Children.Add(imTA);
        }

        /* 图片移动的动画
         * sb为新建动画所在的故事版， to为Margin属性， 
         * time为这段动画的持续时间（以秒为单位）*/
        public void move(Storyboard sb, Thickness to, double time)
        {
            ThicknessAnimation imTA = new ThicknessAnimation();
            imTA.To = to;
            imTA.Duration = TimeSpan.FromSeconds(time);
            Storyboard.SetTargetName(imTA, Name);
            Storyboard.SetTargetProperty(imTA, new PropertyPath(ImageObject.MarginProperty));
            sb.Children.Add(imTA);
        }


        /* 此函数实现图片的放大和缩小
         * sb为新建动画所在的故事版
         * xscaleFrom和yscaleFrom为动画初始宽度和高度
         * xscaleTo和yscaleTo为目标宽度与原宽度的比值和目标高度与原高度的比值
         * seconds为动画的持续时间
         */
        public void changeSize(Storyboard sb, double xscaleFrom, double yscaleFrom,
            double xscaleTo, double yscaleTo, double seconds)
        {

            this.RenderTransform = new ScaleTransform();
            DoubleAnimation xScaleAn = new DoubleAnimation();
            xScaleAn.From = xscaleFrom;
            xScaleAn.To = xscaleTo;
            xScaleAn.Duration = TimeSpan.FromSeconds(seconds);

            DoubleAnimation yScaleAn = new DoubleAnimation();
            yScaleAn.From = yscaleFrom;
            yScaleAn.To = yscaleTo;
            yScaleAn.Duration = TimeSpan.FromSeconds(seconds);

            DependencyProperty[] propertyChain0 = new DependencyProperty[]
            {
                TextObject.RenderTransformProperty,
                ScaleTransform.ScaleXProperty
            };
            Storyboard.SetTargetName(xScaleAn, this.Name);
            Storyboard.SetTargetProperty(xScaleAn, new PropertyPath("(0).(1)", propertyChain0));

            DependencyProperty[] propertyChain1 = new DependencyProperty[]
            {
                TextObject.RenderTransformProperty,
                ScaleTransform.ScaleYProperty
            };
            Storyboard.SetTargetName(yScaleAn, this.Name);
            Storyboard.SetTargetProperty(yScaleAn, new PropertyPath("(0).(1)", propertyChain1));

            sb.Children.Add(xScaleAn);
            sb.Children.Add(yScaleAn);
        }


        /* 此函数实现图片的放大和缩小
         * sb为新建动画所在的故事版，
         * xscaleTo和yscaleTo为目标宽度与原宽度的比值和目标高度与原高度的比值
         * seconds为动画的持续时间
         */
        public void changeSize(Storyboard sb, double xscaleTo, double yscaleTo, double seconds)
        {

            this.RenderTransform = new ScaleTransform();
            DoubleAnimation xScaleAn = new DoubleAnimation();
            xScaleAn.To = xscaleTo;
            xScaleAn.Duration = TimeSpan.FromSeconds(seconds);

            DoubleAnimation yScaleAn = new DoubleAnimation();
            yScaleAn.To = yscaleTo;
            yScaleAn.Duration = TimeSpan.FromSeconds(seconds);

            DependencyProperty[] propertyChain0 = new DependencyProperty[]
            {
                TextObject.RenderTransformProperty,
                ScaleTransform.ScaleXProperty
            };
            Storyboard.SetTargetName(xScaleAn, this.Name);
            Storyboard.SetTargetProperty(xScaleAn, new PropertyPath("(0).(1)", propertyChain0));

            DependencyProperty[] propertyChain1 = new DependencyProperty[]
            {
                TextObject.RenderTransformProperty,
                ScaleTransform.ScaleYProperty
            };
            Storyboard.SetTargetName(yScaleAn, this.Name);
            Storyboard.SetTargetProperty(yScaleAn, new PropertyPath("(0).(1)", propertyChain1));

            sb.Children.Add(xScaleAn);
            sb.Children.Add(yScaleAn);
        }


        /*sb为所在故事版，
         * from为初始角度，
         * to为目标角度，
         * seconds为动画持续时间*/
        public void rotate(Storyboard sb, double from, double to, double seconds)
        {
            RotateTransform rt = new RotateTransform();
            rt.CenterX = 200;
            rt.CenterY = 300;
            this.RenderTransform = rt;
            //this.RenderTransform.SetValue(RotateTransform.CenterXProperty);
            //this.RenderTransform.SetValue(RotateTransform.CenterXProperty, 200);

            DoubleAnimation angleAn = new DoubleAnimation();
            angleAn.From = from;
            angleAn.To = to;
            angleAn.Duration = TimeSpan.FromSeconds(seconds);

            DependencyProperty[] propertyChain = new DependencyProperty[]
            {
                DataGrid.RenderTransformProperty,
                RotateTransform.AngleProperty
            };
            Storyboard.SetTargetName(angleAn, this.Name);
            Storyboard.SetTargetProperty(angleAn, new PropertyPath("(0).(1)", propertyChain));

            sb.Children.Add(angleAn);
        }
    }
}
